package com.uuah.server.transformer;

import com.uuah.server.api.IUuahMessage;
import com.uuah.server.exception.transformer.TransformerException;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午02:06:50
 * @version 1.0.0
 */
public class TransformerTemplate extends AbstractMessageAwareTransformer {
	private TransformerCallback callback;

	public TransformerTemplate(TransformerCallback callback) {
		this.callback = callback;
	}

	public Object transform(IUuahMessage message, String outputEncoding)
			throws TransformerException {
		try {
			return callback.doTransform(message);
		} catch (TransformerException e) {
			throw e;
		} catch (Exception e) {
			throw new TransformerException(this, e);
		}
	}

	public interface TransformerCallback {
		public Object doTransform(IUuahMessage message) throws Exception;
	}

	public static class OverwitePayloadCallback implements TransformerCallback {
		private Object payload;

		public OverwitePayloadCallback(Object payload) {
			this.payload = payload;
		}

		public Object doTransform(IUuahMessage message) throws Exception {
			return payload;
		}
	}
}
